import java.lang.reflect.Field;


import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.NotesException;
import lotus.domino.WebServiceBase;

public class WsForZbProject{

  // This is a template implementation class for your web service. It
  // becomes extraneous if you import a WSDL document. Consumers of this
  // web service can call any public method in the implementation class.
  // 
  // To obtain a Session object use this code:
  // Session s = WebServiceBase.getCurrentSession();
	
	//public static final String OK = "OK";
	
/*	public String getAllProjectName(String strFY,String strType) throws NotesException{
		String result ="";
		Database db = WebServiceBase.getCurrentSession().getCurrentDatabase();
		DocumentCollection dc = db.search("form= \"frmWebFlow\" & strProjectCat = \"" +strType +"\" & strProjectStage = \"" +strFY +"\"");
		//result.
		
		return result;
	}*/

	
	
	//根据项目年度和类型得到所有项目名称返回
	public String getProjectNames(String strFY,String strType) throws NotesException{
		System.out.println(strFY.concat("||".concat(strType)));
		String result ="<projects>";
		Database db = WebServiceBase.getCurrentSession().getCurrentDatabase();
		String strFormal= "form= \"frmProject_ex\" & strProjectCat = \"" +strType +"\" & strProjectStage = \"" +strFY +"\"";
		DocumentCollection dc = db.search(strFormal);
		Document doc = dc.getFirstDocument();
		
		
		while(doc != null){
			result +="<project>"+doc.getItemValueString("strProjectName").trim()+"</project>";
			doc = dc.getNextDocument(doc);
		}
		result +="</projects>";
		
		System.out.print(result);
		
		return result;
	}
	
	
	/*	  
	 * For purchase system update project implement infomation.
	 * */
	public String updateProjectImplement(ProjectImplementInfo pii) throws IllegalArgumentException, IllegalAccessException{
		
		//System.out.println("[====================Webservice Running=============================================]");
		//return "IS_OK";
		Document doc;
		
		try {
			//System.out.println("form= \"frmWebFlow\" & XMBH = \"" +pii.getXMBH() +"\"");
			Database db = WebServiceBase.getCurrentSession().getCurrentDatabase();
			DocumentCollection dc = db.search("form= \"frmWebFlow\" & XMBH = \"" +pii.getXMBH() +"\"");
			//DocumentCollection dc = db.search("form= \"frmWebFlow\" & XMBH = \"" +project +"\"");
			//根据项目编号查询到项目行信息则获得项目文档。否则返回未发现
			if(dc.getCount()>0){
				doc  = dc.getFirstDocument();				
			}else{
				return "NotFound";
			}
			
		 //通过反射取得对象所有域值和项目执行文档中域值做比较
		 Class cls = pii.getClass();
		 //对象的属性列表
	     Field[] fields = cls.getDeclaredFields();
	     for(int i=0; i<fields.length; i++){
	            Field f = fields[i];
	            //设置
	            f.setAccessible(true);
	            //如果对象属性新值和文档原值相等不做操作 后台输出域值
	            if(f.get(pii)!= null){
		            if(f.get(pii).toString().equals(doc.getItemValueString(f.getName()))){
			            System.out.println("[属性名:" + f.getName() + " 属性值:" + f.get(pii) + "无变化]");	
		            }else{
		            //否则用对象属性值更新文档域值(对象属性名称要设置和文档域值名称一样。)
		            	System.out.println("[属性名:" + f.getName() + " 属性值:" + f.get(pii) + "已更新");
		            	doc.replaceItemValue(f.getName(), f.get(pii));
		            }
	            }
	     }
	     doc.save(true, false);
	     
		} catch (NotesException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
			
			return "Error";
		}
	     
		return "IS_OK";
	}
	
	
	public String testFunction(String parm){
		
		return "TestOK";
	}
	
}